package com.unsky.utils.vo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.util.StringUtils;


/**
 * 分页ＤＴＯ
 * 
 * @project yuntong
 * @author unsky
 * @date 2016年10月27日 上午9:14:23
 * @copyRight 山东阿帕网络技术有限公司
 * @webSite http://www.kt56.com
 */
public class PageVO{
	private int page;
	private int pageSize;
	private String sortType = "createDate";
	private String sortOrder = "desc";
	public  final String ORDER_DESC="desc";//降序
	public  final String ORDER_ASC="asc";//降序
	
	public PageVO() {
		super();
	}
	public int getPage() {
		return page>=1?page:1;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getPageSize() {
		return pageSize>=1?pageSize:5;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public String getSortType() {
		return StringUtils.isEmpty(sortType)?"createTime":sortType;
	}
	public void setSortType(String sortType) {
		this.sortType = sortType;
	}
	public String getSortOrder() {
		return StringUtils.isEmpty(sortOrder)?"desc":sortOrder;
	}
	public void setSortOrder(String sortOrder) {
		this.sortOrder = sortOrder;
	}
	/**
	 * 获取查询的分页
	 * 支持多字段排序
	 * @return
	 * @Date 2016年10月27日 上午9:20:46
	 */
	public Pageable getPageable(){
		List<Sort.Order> orderList = new ArrayList<Sort.Order>();
		String[] sorts = getSortType().split(",");
		String[] sortOrders = getSortOrder().split(",");
		for (int i = 0; i < sorts.length; i++) {
			Direction driection=Direction.ASC;
			if(sortOrders.length>i){
				if(ORDER_DESC.equals(sortOrders[i])){
					driection=Direction.DESC;
				}
			}
			Sort.Order order = new Sort.Order(driection, sorts[i]);
			orderList.add(order);
		}
		Pageable pageable = new PageRequest(getPage()-1, 
				getPageSize(), new Sort(orderList));
		return pageable;
	}
}
